package cc.linker.steplink.crm.merchant.controller;


import cc.linker.steplink.crm.commons.coupon.CouponCustomerInfo;
import cc.linker.steplink.crm.commons.coupon.CouponRequest;
import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.commons.service.CommonsService;
import cc.linker.steplink.crm.merchant.pojo.coupon.SendCouponDto;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.merchant.common.PcCouponCustomer;
import cc.linker.steplink.crm.merchant.service.CouponPcService;
import cc.linker.steplink.crm.merchant.service.acs.MerchantInfoService;
import cc.linker.steplink.crm.merchanth5.service.redis.CustomerCacheService;
import cc.linker.steplink.crm.response.GlobalException;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/pc/coupon")
public class CouponPcController {

    @Autowired
    private CouponRequest couponRequest;
    @Autowired
    private PcCouponCustomer pcCouponCustomer;
    @Autowired
    private CouponPcService couponPcService;
    @Autowired
    private MerchantInfoService merchantInfoService;
    @Autowired
    private CommonsService commonsService;
    @Autowired
    private UserInfoUtil userInfoUtil;
    @Autowired
    private CustomerCacheService customerCacheService;


    /**
     * @Description：系统给用户发券
     * @DateTime：2020/8/20 13:07
     * @Params：
     * @Return：
     */
    @PostMapping("/sendCoupon")
    public void sendCoupon(@RequestBody SendCouponDto sendInfo, HttpServletRequest request) {
        this.couponPcService.sendCoupon(sendInfo, request);
    }




    @GetMapping("/getCouponList")
    public Object getCouponList(
                          @RequestParam(value = "pageIndex", defaultValue = "1", required = false) Integer pageIndex,
                          @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                          @RequestParam(value = "name", defaultValue = "", required = false) String name,
                          @RequestParam(value = "type", defaultValue = "", required = false) Integer type,
                          @RequestParam(value = "startTime", defaultValue = "", required = false) String startTime,
                          @RequestParam(value = "endTime", defaultValue = "", required = false) String endTime,
                          @RequestParam(value = "publishStartTime", defaultValue = "", required = false) String publishStartTime,
                          @RequestParam(value = "publishEndTime", defaultValue = "", required = false) String publishEndTime,
                          @RequestParam(value = "status", defaultValue = "", required = false) Integer status,
                          @RequestParam(value = "notEnd", defaultValue = "", required = false) Integer notEnd,
                          @RequestParam(value = "both", defaultValue = "0", required = false) Integer both,
                          @RequestParam(value = "useScene", defaultValue = "", required = false) Integer useScene,
                          @RequestParam(value = "customerType", defaultValue = "", required = false) Integer customerType,
                          String orgId

    ) {
        Map<String, Object> param = new HashMap<>();
        param.put("pageIndex", pageIndex);
        param.put("pageSize", pageSize);
        if(both == 1){
            List<String> salerIds = couponPcService.getSalerId(orgId);
            if(null!=salerIds && salerIds.size()>0){
                param.put("salerIds",String.join(",", salerIds));
            }
        }
        if (!StringUtils.isEmpty(name)) {
            param.put("name", name);
        }
        if (!StringUtils.isEmpty(status)) {
            param.put("status", status);
        }
        if (!StringUtils.isEmpty(notEnd)) {
            param.put("notEnd", notEnd);
        }
        if (!StringUtils.isEmpty(useScene)) {
            param.put("useScene", useScene);
        }
        if (!StringUtils.isEmpty(customerType)) {
            Map<String,Object> customerTypeParam = new HashMap<>();
            customerTypeParam.put("orgId",orgId);
            customerTypeParam.put("customerType",customerType);
            List<String> couponIds = couponPcService.getCouponIdByCustomerType(customerTypeParam);
            if(null == couponIds || couponIds.size() == 0){
                return new PageInfo<>(new ArrayList<>());
            }else{
                param.put("couponIds", String.join(",", couponIds));
            }
        }
        if (!StringUtils.isEmpty(type)) {
            param.put("type", type);
        }
        if (!StringUtils.isEmpty(startTime)) {
            param.put("startTime", startTime);
        }
        if (!StringUtils.isEmpty(endTime)) {
            param.put("endTime", endTime);
        }
        if (!StringUtils.isEmpty(publishStartTime)) {
            param.put("publishStartTime", publishStartTime);
        }
        if (!StringUtils.isEmpty(publishEndTime)) {
            param.put("publishEndTime", publishEndTime);
        }
        Map data = new HashMap();
        CouponCustomerInfo couponCustomerInfo = CouponCustomerInfo
                .builder()
                .customerId(orgId)
                .cover("")
                .customerName("")
                .orgId(orgId)
                .build();
        Object obj = couponRequest.doRequest("coupon-page-list", param, couponCustomerInfo);
        if(null != obj){
            data = (Map)obj;
        }
        if(!data.isEmpty()){
            List<Map<String,Object>> list = (List<Map<String,Object>>)data.get("list");
            if(null!=list && list.size()>0){
                for(Map<String,Object> da:list){
                    da.put("storeName","");
                    Map<String, Object> createCustomerInfo = new HashMap<>();
                    if(orgId.equals(da.get("createCustomerId").toString())){
                        da.put("isSelf",1);
                        createCustomerInfo = customerCacheService.getCustomerCacheStoreInfo(orgId,da.get("createCustomerId").toString(),"1");
                    }else{
                        da.put("isSelf",0);
                        createCustomerInfo = customerCacheService.getCustomerCacheStoreInfo(orgId,da.get("createCustomerId").toString(),"2");
                    }
                    if(createCustomerInfo!=null){
                        da.put("storeName",createCustomerInfo.get("storeName"));
                    }

                    Map<String, Object> couponGoodsData = couponPcService.getCouponGoodsData(da.get("couponId").toString());
                    if(null!=couponGoodsData){
                        da.put("customerType",couponGoodsData.get("customerType"));
                        da.put("goodsType",couponGoodsData.get("goodsType"));
                        da.put("customerCouponNum",couponGoodsData.get("customerCouponNum"));
                    }
                }
            }
            data.put("list",list);
        }
        return data;
    }
    @GetMapping("/getlist")
    public Object getList(HttpServletRequest request,
                          @RequestParam(value = "pageIndex", defaultValue = "1", required = false) Integer pageIndex,
                          @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                          @RequestParam(value = "name", defaultValue = "", required = false) String name,
                          @RequestParam(value = "type", defaultValue = "", required = false) Integer type,
                          @RequestParam(value = "startTime", defaultValue = "", required = false) String startTime,
                          @RequestParam(value = "endTime", defaultValue = "", required = false) String endTime,
                          @RequestParam(value = "publishStartTime", defaultValue = "", required = false) String publishStartTime,
                          @RequestParam(value = "publishEndTime", defaultValue = "", required = false) String publishEndTime,
                          @RequestParam(value = "status", defaultValue = "", required = false) Integer status,
                          @RequestParam(value = "notEnd", defaultValue = "", required = false) Integer notEnd,
                          @RequestParam(value = "both", defaultValue = "0", required = false) Integer both,
                          @RequestParam(value = "useScene", defaultValue = "", required = false) Integer useScene,
                          @RequestParam(value = "customerType", defaultValue = "", required = false) Integer customerType

    ) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Map<String, Object> param = new HashMap<>();
        param.put("pageIndex", pageIndex);
        param.put("pageSize", pageSize);
        if(!userinfo.getSaler() && both == 1){
            List<String> salerIds = couponPcService.getSalerId(userinfo.getOrgId());
            if(null!=salerIds && salerIds.size()>0){
                param.put("salerIds",String.join(",", salerIds));
            }
        }
        if (!StringUtils.isEmpty(name)) {
            param.put("name", name);
        }
        if (!StringUtils.isEmpty(status)) {
            param.put("status", status);
        }
        if (!StringUtils.isEmpty(notEnd)) {
            param.put("notEnd", notEnd);
        }
        if (!StringUtils.isEmpty(useScene)) {
            param.put("useScene", useScene);
        }
        if (!StringUtils.isEmpty(customerType)) {
            Map<String,Object> customerTypeParam = new HashMap<>();
            customerTypeParam.put("orgId",userinfo.getOrgId());
            customerTypeParam.put("customerType",customerType);
            List<String> couponIds = couponPcService.getCouponIdByCustomerType(customerTypeParam);
            if(null == couponIds || couponIds.size() == 0){
                return new PageInfo<>(new ArrayList<>());
            }else{
                param.put("couponIds", String.join(",", couponIds));
            }
        }
        if (!StringUtils.isEmpty(type)) {
            param.put("type", type);
        }
        if (!StringUtils.isEmpty(startTime)) {
            param.put("startTime", startTime);
        }
        if (!StringUtils.isEmpty(endTime)) {
            param.put("endTime", endTime);
        }
        if (!StringUtils.isEmpty(publishStartTime)) {
            param.put("publishStartTime", publishStartTime);
        }
        if (!StringUtils.isEmpty(publishEndTime)) {
            param.put("publishEndTime", publishEndTime);
        }
        Map data = new HashMap();
        Object obj = couponRequest.doRequest("coupon-page-list", param, pcCouponCustomer.getCouponCustomerInfo(request));
        if(null != obj){
            data = (Map)obj;
        }
        if(!data.isEmpty()){
            List<Map<String,Object>> list = (List<Map<String,Object>>)data.get("list");
            if(null!=list && list.size()>0){
                for(Map<String,Object> da:list){
                    da.put("storeName","");
                    Map<String, Object> createCustomerInfo = new HashMap<>();
                    if(userinfo.getSaler()){
                        if(userinfo.getCustomerId().equals(da.get("createCustomerId").toString())){
                            da.put("isSelf",1);
                            createCustomerInfo = customerCacheService.getCustomerCacheStoreInfo(userinfo.getOrgId(), da.get("createCustomerId").toString(), "2");
                        }
                    }else{
                        if(userinfo.getOrgId().equals(da.get("createCustomerId").toString())){
                            da.put("isSelf",1);
                            createCustomerInfo = customerCacheService.getCustomerCacheStoreInfo(userinfo.getOrgId(),da.get("createCustomerId").toString(),"1");
                        }else{
                            da.put("isSelf",0);
                            createCustomerInfo = customerCacheService.getCustomerCacheStoreInfo(userinfo.getOrgId(),da.get("createCustomerId").toString(),"2");
                        }
                    }
                    if(createCustomerInfo!=null){
                        da.put("storeName",createCustomerInfo.get("storeName"));
                    }

                    Map<String, Object> couponGoodsData = couponPcService.getCouponGoodsData(da.get("couponId").toString());
                    if(null!=couponGoodsData){
                        da.put("customerType",couponGoodsData.get("customerType"));
                        da.put("goodsType",couponGoodsData.get("goodsType"));
                        da.put("customerCouponNum",couponGoodsData.get("customerCouponNum"));
                    }
                }
            }
            data.put("list",list);
        }
        return data;
    }

    @GetMapping("/getInfo")
    public Object getInfo(HttpServletRequest request,String couponId){
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        if(StringUtils.isEmpty(couponId)){
            throw GlobalException.build("参数错误");
        }
        Map<String, Object> param = new HashMap<>();
        param.put("couponId", couponId);

        Map<String,Object> res = couponPcService.getInfo(param,request);
        Map<String, Object> couponGoodsData = couponPcService.getCouponGoodsData(param.get("couponId").toString());
        if(null!=couponGoodsData){
            if(!"0".equals(couponGoodsData.get("goodsIds").toString())){
                int sale = 0;
                if(userinfo.getSaler()){
                    sale = 1;
                }
                List<Map<String,Object>> goodsList = couponPcService.getGoodsList(couponGoodsData.get("goodsIds").toString(),sale,userinfo.getOrgId());
                res.put("goodsList",goodsList);
            }else{
                res.put("goodsList",new ArrayList<>());
            }
            res.put("goodsIds",couponGoodsData.get("goodsIds").toString().split(","));
            res.put("goodsType",couponGoodsData.get("goodsType"));
            res.put("customerCouponNum",couponGoodsData.get("customerCouponNum"));
            res.put("customerType",couponGoodsData.get("customerType"));
            res.put("groupId",couponGoodsData.get("groupId"));
        }else{
            res.put("goodsList",new ArrayList<>());
            res.put("goodsIds",new ArrayList<>());
            res.put("goodsType",0);
            res.put("customerCouponNum",0);
            res.put("customerType",0);
        }
        return res;
    }


    @GetMapping("/getdetail")
    public Object getDetail(HttpServletRequest request,
                            @RequestParam(value = "pageIndex", defaultValue = "1", required = false) Integer pageIndex,
                            @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                            String couponId,
                            @RequestParam(value = "startTime", defaultValue = "", required = false) String startTime,
                            @RequestParam(value = "endTime", defaultValue = "", required = false) String endTime){
        if(StringUtils.isEmpty(couponId)){
            throw GlobalException.build("参数错误");
        }
        Map<String, Object> param = new HashMap<>();
        param.put("pageIndex", pageIndex);
        param.put("pageSize", pageSize);
        param.put("couponId", couponId);
        if (!StringUtils.isEmpty(startTime)) {
            param.put("startTime", startTime);
        }
        if (!StringUtils.isEmpty(endTime)) {
            param.put("endTime", endTime);
        }
        return couponRequest.doRequest("coupon-data-detail", param, pcCouponCustomer.getCouponCustomerInfo(request));
    }


    @PostMapping("/end")
    public Object end(HttpServletRequest request,@RequestBody Map<String, Object> param){
        if(StringUtils.isEmpty(param.get("couponId"))){
            throw GlobalException.build("参数错误");
        }
        return couponRequest.doRequest("coupon-end", param, pcCouponCustomer.getCouponCustomerInfo(request));
    }

    @PostMapping("/del")
    public Object del(HttpServletRequest request,@RequestBody Map<String, Object> param){
        if(StringUtils.isEmpty(param.get("couponId"))){
            throw GlobalException.build("参数错误");
        }
        return couponRequest.doRequest("coupon-del", param, pcCouponCustomer.getCouponCustomerInfo(request));
    }

    @PostMapping("/add")
    public void add(HttpServletRequest request,@RequestBody Map<String, Object> param) throws ParseException {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        if(StringUtils.isEmpty(param.get("startTime"))){
            param.remove("startTime");
        }
        if(StringUtils.isEmpty(param.get("endTime"))){
            param.remove("endTime");
        }
        String orgId = userinfo.getOrgId();
        param.put("orgId",orgId);
        boolean ifSaler = userinfo.getSaler();
        String customerId = orgId;
        param.put("sale", 0);
        if (ifSaler) {
            customerId = merchantInfoService.getCustomerIdByAdminId(userinfo.getAdminId());
            param.put("sale", 1);
        }
        param.put("customerId", customerId);

        String useScene =param.get("useScene")==null ? "0": param.get("useScene").toString();
        if("1".equals(useScene)){
            Map data = (Map)couponRequest.doRequest("coupon-add", param, pcCouponCustomer.getCouponCustomerInfo(request));
            data.put("orgId",orgId);
            data.put("goodsType",3);
            data.put("customerTypeCrm",param.get("customerType"));
            data.put("customerCouponNumCrm",param.get("customerCouponNum"));
            data.put("groupIdCrm",param.get("groupId"));
            if(userinfo.getSaler()){
                data.put("beLongId",merchantInfoService.getCustomerIdByAdminId(userinfo.getAdminId()));
            }else{
                data.put("beLongId",orgId);
            }
            couponPcService.addCouponGoodsData(data);
        }else{
            List<Integer> goodsIds = checkParam(param);
            param.remove("discountNum");
            param.remove("discountPrice");
            param.remove("maxDiscountPrice");
            param.remove("minUsePrice");
            Map data = (Map)couponRequest.doRequest("coupon-add", param, pcCouponCustomer.getCouponCustomerInfo(request));
            data.put("orgId",orgId);
            data.put("goodsIds",goodsIds);
            data.put("goodsType",param.get("goodsType"));
            data.put("customerTypeCrm",param.get("customerType"));
            data.put("customerCouponNumCrm",param.get("customerCouponNum"));
            data.put("groupIdCrm",param.get("groupId"));
            if(userinfo.getSaler()){
                data.put("beLongId",merchantInfoService.getCustomerIdByAdminId(userinfo.getAdminId()));
            }else{
                data.put("beLongId",orgId);
            }
            couponPcService.addCouponGoodsData(data);
        }




//        if("0".equals(type) || "1".equals(type)){
//            //判断亏损
//            List<Integer> goodsIds = checkParam(param);
//            param.remove("discountNum");
//            param.remove("discountPrice");
//            param.remove("maxDiscountPrice");
//            param.remove("minUsePrice");
//            Map data = (Map)couponRequest.doRequest("coupon-add", param, pcCouponCustomer.getCouponCustomerInfo(request));
//            data.put("orgId",orgId);
//            data.put("goodsIds",goodsIds);
//            data.put("goodsType",param.get("goodsType"));
//            data.put("customerTypeCrm",param.get("customerType"));
//            data.put("customerCouponNumCrm",param.get("customerCouponNum"));
//            if(userinfo.getSaler()){
//                data.put("beLongId",merchantInfoService.getCustomerIdByAdminId(userinfo.getAdminId()));
//            }else{
//                data.put("beLongId",orgId);
//            }
//            couponPcService.addCouponGoodsData(data);
//        }else{
//            Map data = (Map)couponRequest.doRequest("coupon-add", param, pcCouponCustomer.getCouponCustomerInfo(request));
//            data.put("orgId",orgId);
//            data.put("goodsType",3);
//            data.put("customerTypeCrm",param.get("customerType"));
//            data.put("customerCouponNumCrm",param.get("customerCouponNum"));
//            if(userinfo.getSaler()){
//                data.put("beLongId",merchantInfoService.getCustomerIdByAdminId(userinfo.getAdminId()));
//            }else{
//                data.put("beLongId",orgId);
//            }
//            couponPcService.addCouponGoodsData(data);
//        }
    }

    @PostMapping("/save")
    public void save(HttpServletRequest request,@RequestBody Map<String, Object> param) throws ParseException {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        param.put("orgId",orgId);
        if(StringUtils.isEmpty(param.get("couponId"))){
            throw GlobalException.build("参数错误");
        }
        if(StringUtils.isEmpty(param.get("startTime"))){
            param.remove("startTime");
        }
        if(StringUtils.isEmpty(param.get("endTime"))){
            param.remove("endTime");
        }
        param.put("orgId",orgId);
        boolean ifSaler = userinfo.getSaler();
        String customerId = orgId;
        param.put("sale", 0);
        if (ifSaler) {
            customerId = merchantInfoService.getCustomerIdByAdminId(userinfo.getAdminId());
            param.put("sale", 1);
        }
        param.put("customerId", customerId);

        String useScene = param.get("useScene").toString();
        if("1".equals(useScene)){
            Map data = (Map)couponRequest.doRequest("coupon-save", param, pcCouponCustomer.getCouponCustomerInfo(request));
            data.put("orgId",orgId);
            data.put("goodsType",3);
            data.put("customerTypeCrm",param.get("customerType"));
            data.put("customerCouponNumCrm",param.get("customerCouponNum"));
            data.put("groupIdCrm",param.get("groupId"));
            if(userinfo.getSaler()){
                data.put("beLongId",merchantInfoService.getCustomerIdByAdminId(userinfo.getAdminId()));
            }else{
                data.put("beLongId",orgId);
            }
            couponPcService.saveCouponGoodsData(data);
        }else{
            //判断亏损
            List<Integer> goodsIds = checkParam(param);
            param.remove("discountNum");
            param.remove("discountPrice");
            param.remove("maxDiscountPrice");
            param.remove("minUsePrice");
            Map data = (Map)couponRequest.doRequest("coupon-save", param, pcCouponCustomer.getCouponCustomerInfo(request));
            data.put("orgId",orgId);
            data.put("goodsIds",goodsIds);
            data.put("goodsType",param.get("goodsType"));
            data.put("customerTypeCrm",param.get("customerType"));
            data.put("customerCouponNumCrm",param.get("customerCouponNum"));
            data.put("groupIdCrm",param.get("groupId"));
            if(userinfo.getSaler()){
                data.put("beLongId",merchantInfoService.getCustomerIdByAdminId(userinfo.getAdminId()));
            }else{
                data.put("beLongId",orgId);
            }
            couponPcService.saveCouponGoodsData(data);
        }


//        String type = param.get("type").toString();
//        if("0".equals(type) || "1".equals(type)){
//            //判断亏损
//            List<Integer> goodsIds = checkParam(param);
//            param.remove("discountNum");
//            param.remove("discountPrice");
//            param.remove("maxDiscountPrice");
//            param.remove("minUsePrice");
//            Map data = (Map)couponRequest.doRequest("coupon-save", param, pcCouponCustomer.getCouponCustomerInfo(request));
//            data.put("orgId",orgId);
//            data.put("goodsIds",goodsIds);
//            data.put("goodsType",param.get("goodsType"));
//            data.put("customerTypeCrm",param.get("customerType"));
//            data.put("customerCouponNumCrm",param.get("customerCouponNum"));
//            if(userinfo.getSaler()){
//                data.put("beLongId",merchantInfoService.getCustomerIdByAdminId(userinfo.getAdminId()));
//            }else{
//                data.put("beLongId",orgId);
//            }
//            couponPcService.saveCouponGoodsData(data);
//        }else{
//            Map data = (Map)couponRequest.doRequest("coupon-save", param, pcCouponCustomer.getCouponCustomerInfo(request));
//            data.put("orgId",orgId);
//            data.put("goodsType",3);
//            data.put("customerTypeCrm",param.get("customerType"));
//            data.put("customerCouponNumCrm",param.get("customerCouponNum"));
//            if(userinfo.getSaler()){
//                data.put("beLongId",merchantInfoService.getCustomerIdByAdminId(userinfo.getAdminId()));
//            }else{
//                data.put("beLongId",orgId);
//            }
//            couponPcService.saveCouponGoodsData(data);
//        }

    }

    @GetMapping("/allcouponlist")
    public Object allcouponlist(HttpServletRequest request,String goodsId){
        Map<String, Object> param = new HashMap<>();
        List<String> couponIds= couponPcService.getCouponIdByGoodsId(goodsId);
        List<Map<String, Object>> dataList = null;
        if(couponIds!=null && couponIds.size()>0){
            param.put("couponIds",String.join(",",couponIds));
            Object list = couponRequest.doRequest("coupon-all-list", param, pcCouponCustomer.getCouponCustomerInfo(request));
            dataList = (List<Map<String, Object>>) list;
        }

        return dataList;
    }

    @GetMapping("/exportcouponlist")
    public void exportCouponList(HttpServletRequest request,
                                 @RequestParam(value = "name", defaultValue = "", required = false) String name,
                                 @RequestParam(value = "type", defaultValue = "", required = false) Integer type,
                                 @RequestParam(value = "startTime", defaultValue = "", required = false) String startTime,
                                 @RequestParam(value = "endTime", defaultValue = "", required = false) String endTime,
                                 @RequestParam(value = "publishStartTime", defaultValue = "", required = false) String publishStartTime,
                                 @RequestParam(value = "publishEndTime", defaultValue = "", required = false) String publishEndTime,
                                 @RequestParam(value = "status", defaultValue = "", required = false) Integer statusIn,
                                 @RequestParam(value = "both", defaultValue = "0", required = false) Integer both,
                                 @RequestParam(value = "useScene", defaultValue = "", required = false) Integer useScene,
                                 @RequestParam(value = "customerType", defaultValue = "", required = false) Integer customerType,
                                 HttpServletResponse response,
                                 @RequestParam(value = "fileName") String fileName
    ) throws Exception {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Map<String, Object> param = new HashMap<>();
//        if(!userinfo.getSaler()){
//            List<String> salerIds = couponPcService.getSalerId(userinfo.getOrgId());
//            if(null!=salerIds && salerIds.size()>0){
//                param.put("salerIds",salerIds);
//            }
//        }
        if (!StringUtils.isEmpty(name)) {
            param.put("name", name);
        }
        if (!StringUtils.isEmpty(statusIn)) {
            param.put("status", statusIn);
        }
        String fNameType = "全部";
        if (!StringUtils.isEmpty(type)) {
            if(0 == type){
                fNameType = "满减券";
            }
            if(1 == type){
                fNameType = "折扣券";
            }
            if(2 == type){
                fNameType = "抵扣券";
            }
            if(3 == type){
                fNameType = "礼品券";
            }
            param.put("type", type);
        }
        if (!StringUtils.isEmpty(startTime)) {
            param.put("startTime", startTime);
        }
        if (!StringUtils.isEmpty(endTime)) {
            param.put("endTime", endTime);
        }
        if (StringUtils.isEmpty(fileName)) {
            fileName = "卡券数据总览";
        }

        if (!StringUtils.isEmpty(useScene)) {
            param.put("useScene", useScene);
        }
        boolean doRequest = true;
        if (!StringUtils.isEmpty(customerType)) {
            Map<String,Object> customerTypeParam = new HashMap<>();
            customerTypeParam.put("orgId",userinfo.getOrgId());
            customerTypeParam.put("customerType",customerType);
            List<String> couponIds = couponPcService.getCouponIdByCustomerType(customerTypeParam);
            if(null == couponIds || couponIds.size() == 0){
                doRequest = false;
            }else{
                param.put("couponIds",String.join(",",couponIds));
            }
        }
        if (!StringUtils.isEmpty(publishStartTime)) {
            param.put("publishStartTime", publishStartTime);
        }
        if (!StringUtils.isEmpty(publishEndTime)) {
            param.put("publishEndTime", publishEndTime);
        }



        //private Integer type;//优惠券类型 0 满减 1折扣
//        if ("满减优惠券".equals(fileName)) {
//            param.put("type", 0);
//        } else if ("折扣优惠券".equals(fileName)) {
//            param.put("type", 1);
//        }
        List<Map<String, Object>> dataList = new ArrayList<>();
        if(doRequest){
            Object list = couponRequest.doRequest("coupon-all-list", param, pcCouponCustomer.getCouponCustomerInfo(request));
            dataList = (List<Map<String, Object>>) list;
        }

        if ("卡券数据总览".equals(fileName)) {
            int i = 1;
            for (Map<String, Object> da : dataList) {
                if("0".equals(da.get("type").toString())){
                    da.put("typeStr", "满减券");
                }
                if ("1".equals(da.get("type").toString())) {
                    da.put("typeStr", "折扣券");
                }
                if ("2".equals(da.get("type").toString())) {
                    da.put("typeStr", "抵扣券");
                }
                if ("3".equals(da.get("type").toString())) {
                    da.put("typeStr", "礼品券");
                }
                da.put("num", i);
                i++;
            }
            String[] key = {"num", "name", "typeStr", "maxPublishNum", "receiveNum", "useNum", "payNum"};
            String[] title = {"序号", "卡券名称", "卡券类型", "发行数量", "已领取", "已使用", "订单总数"};
            commonsService.excelDownload(dataList, key, title, fileName, response);
        } else {
            int i = 1;
            for (Map<String, Object> da : dataList) {
                Map<String, Object> couponGoodsData = couponPcService.getCouponGoodsData(da.get("couponId").toString());
                if (null != couponGoodsData) {
                    String customerTypeDa = couponGoodsData.get("customerType").toString();
                    String cTypeStr = "";
                    if ("0".equals(customerTypeDa)) {
                        cTypeStr = "全部用户";
                    }
                    if ("1".equals(customerTypeDa)) {
                        cTypeStr = "新用户";
                    }
                    if ("2".equals(customerTypeDa)) {
                        cTypeStr = "老用户";
                    }
//                    String goodsType = couponGoodsData.get("goodsType").toString();
//                    String gTypeStr = "";
//                    if ("0".equals(goodsType)) {
//                        gTypeStr = "全部商品";
//                    }
//                    if ("1".equals(goodsType)) {
//                        gTypeStr = "部分商品可用";
//                    }
//                    if ("2".equals(goodsType)) {
//                        gTypeStr = "部分商品不可用";
//                    }

                    String customerCouponNum = couponGoodsData.get("customerCouponNum").toString();
                    String ccn = "";
                    if ("-1".equals(customerCouponNum)) {
                        ccn = "不限制";
                    } else {
                        ccn = customerCouponNum;
                    }
                    da.put("cTypeStr", cTypeStr);
                    da.put("ccn", ccn);
                } else {
                    da.put("cTypeStr", "");
                    da.put("ccn", "");
                }

                String couCon = "";
                String typeStr = "";
                if("0".equals(da.get("type").toString())){
                    if ("0".equals(da.get("minUsePrice").toString())) {
                        couCon = "无门槛，减" + da.get("discountPriceYuan");
                    } else {
                        couCon = "满" + da.get("minUsePriceYuan") + "，减" + da.get("discountPriceYuan");
                    }
                    typeStr = "满减券";
                }
                if ("1".equals(da.get("type").toString())) {
                    if ("0".equals(da.get("minUsePrice").toString())) {
                        couCon = "无门槛，享" + da.get("discountNumStr") + "折";
                    } else {
                        couCon = "满" + da.get("minUsePriceYuan") + "，享" + da.get("discountNumStr") + "折";
                    }
                    typeStr = "折扣券";
                }
                if ("2".equals(da.get("type").toString())) {
                    couCon = da.get("discountPriceYuan") + "元";
                    typeStr = "抵扣券";
                }
                if ("3".equals(da.get("type").toString())) {
                    couCon = "指定礼品";
                    typeStr = "礼品券";
                }
                da.put("typeStr", typeStr);
                da.put("couCon", couCon);
                da.put("num", i);
                i++;
                String status = da.get("status").toString();
                String staStr = "";
                if ("0".equals(status)) {
                    staStr = "未开始";
                }
                if ("1".equals(status)) {
                    staStr = "领取中";
                }
                if ("2".equals(status)) {
                    staStr = "已结束";
                }
                da.put("staStr", staStr);

                if("0".equals(da.get("useScene").toString())){
                    da.put("useSceneStr","线上");
                }
                if("1".equals(da.get("useScene").toString())){
                    da.put("useSceneStr","线下");
                }
                if("2".equals(da.get("useScene").toString())){
                    da.put("useSceneStr","通用");
                }
            }
            String[] key = {"num", "name", "couCon", "typeStr", "useSceneStr", "cTypeStr", "ccn", "maxPublishNum", "publishNum", "useNum", "staStr"};
            String[] title = {"序号", "卡券名称", "内容", "卡券类型", "使用场景", "领取对象", "领取次数（次/人）", "发行数量", "已领取", "已使用", "状态"};
            commonsService.excelDownload(dataList, key, title, "卡券列表" + fNameType, response);




//            if ("满减优惠券".equals(fileName)) {
//                String[] key = {"num", "name", "couCon", "gTypeStr", "cTypeStr", "ccn", "maxPublishNum", "publishNum", "staStr"};
//                String[] title = {"序号", "优惠券名称", "优惠内容", "适用商品", "领取对象", "领取次数（次/人）", "发行数量", "已领取", "状态"};
//                commonsService.excelDownload(dataList, key, title, fileName, response);
//            } else if ("折扣优惠券".equals(fileName)) {
//                String[] key = {"num", "name", "couCon", "maxDiscountPriceYuan", "gTypeStr", "cTypeStr", "ccn", "maxPublishNum", "publishNum", "staStr"};
//                String[] title = {"序号", "优惠券名称", "优惠内容", "最大优惠金额", "适用商品", "领取对象", "领取次数（次/人）", "发行数量", "已领取", "状态"};
//                commonsService.excelDownload(dataList, key, title, fileName, response);
//            }
        }
    }


    @GetMapping("/exportcoupondata")
    public void exportCouponData(HttpServletRequest request,
                                 String couponId,
                                 @RequestParam(value = "startTime", defaultValue = "", required = false) String startTime,
                                 @RequestParam(value = "endTime", defaultValue = "", required = false) String endTime,
                                 HttpServletResponse response
    ) throws Exception {
        if(StringUtils.isEmpty(couponId)){
            throw GlobalException.build("参数错误");
        }
        Map<String, Object> param = new HashMap<>();
        param.put("couponId", couponId);
        if (!StringUtils.isEmpty(startTime)) {
            param.put("startTime", startTime);
        }
        if (!StringUtils.isEmpty(endTime)) {
            param.put("endTime", endTime);
        }
        List<Map<String,Object>> list = (List)couponRequest.doRequest("coupon-data-list", param, pcCouponCustomer.getCouponCustomerInfo(request));
        Map<String,Object> info = (Map)couponRequest.doRequest("coupon-detail", param, pcCouponCustomer.getCouponCustomerInfo(request));
        String fileName = info.get("name")+"卡券数据明细";
        String[] key ;
        String[] title;
        if(null!=info.get("useScene") && "0".equals(info.get("useScene").toString())){
            key = new String[]{"date", "publishNum", "useNum", "payNum", "payMoneyYuan", "payUserNum", "payGoodsNum", "perPriceYuan"};
            title = new String[]{"日期", "领取张数", "使用张数", "支付订单数", "支付总金额", "支付买家数", "支付件数", "客单价"};
        }else{
            for(Map<String,Object> li:list){
                li.put("payMoneyYuan","-");
                li.put("payGoodsNum","-");
                li.put("perPriceYuan","-");
            }
            key = new String[]{"date", "publishNum", "useNum", "payNum", "payMoneyYuan", "payUserNum", "payGoodsNum", "perPriceYuan"};
            title = new String[]{"日期", "领取张数", "使用张数", "支付订单数", "支付总金额", "支付买家数", "支付件数", "客单价"};
        }
        commonsService.excelDownload(list, key, title, fileName, response);
    }


    @GetMapping("/getcouponextension")
    public Map<String,Object> getCouponExtension(HttpServletRequest request,String couponId){
        if(StringUtils.isEmpty(couponId)){
            throw GlobalException.build("参数错误");
        }
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        String adminId = userinfo.getAdminId();
        Boolean ifSales = userinfo.getSaler();
        String storeName;
        if (ifSales) {
            // 联盟商家
            String customerId = merchantInfoService.getCustomerIdByAdminId(adminId);
            storeName = merchantInfoService.customerStoreName(orgId,customerId);
        } else {
            // 大麦用户
            storeName = merchantInfoService.storeName(orgId);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("couponId", couponId);
        Map<String,Object> res = (Map)couponRequest.doRequest("coupon-detail", param, pcCouponCustomer.getCouponCustomerInfo(request));
        res.put("storeName",storeName);
        return res;
    }

    private List<Integer> checkParam(Map param){
        if (!StringUtils.isEmpty(param.get("discountNumStr"))) {
            String discountNumStr = param.get("discountNumStr").toString();
            double discountNum = Double.parseDouble(discountNumStr);
            param.put("discountNum",Math.round(discountNum*100));
        }
        if (!StringUtils.isEmpty(param.get("minUsePriceYuan"))) {
            String minUsePriceYuan = param.get("minUsePriceYuan").toString();
            double minUsePrice = Double.parseDouble(minUsePriceYuan);
            param.put("minUsePrice",Math.round(minUsePrice*100));
        }
        if (!StringUtils.isEmpty(param.get("discountPriceYuan"))) {
            String discountPriceYuan = param.get("discountPriceYuan").toString();
            double discountPrice = Double.parseDouble(discountPriceYuan);
            param.put("discountPrice",Math.round(discountPrice* 100));
        }
        if (!StringUtils.isEmpty(param.get("maxDiscountPriceYuan"))) {
            String maxDiscountPriceYuan = param.get("maxDiscountPriceYuan").toString();
            double maxDiscountPrice = Double.parseDouble(maxDiscountPriceYuan);
            param.put("maxDiscountPrice",Math.round(maxDiscountPrice*100));
        }

        List<Integer> gIdList = new ArrayList<>();
        String goodsType = param.get("goodsType").toString();
        if("0".equals(goodsType)){
            gIdList.add(0);
        }
        //可用商品 //不可用商品
        if("1".equals(goodsType) || "2".equals(goodsType)){
            gIdList = (List)param.get("goodsIds");
        }
        if(gIdList.size()>0){
            String goodsIds = "";
            for (Integer gid:gIdList) {
                if(gid!=0){
                    if(StringUtils.isEmpty(goodsIds)){
                        goodsIds = gid.toString();
                    }else{
                        goodsIds = goodsIds + "," + gid;
                    }
                }
            }
            param.put("goodsIds",goodsIds);
            couponPcService.isLossCoupon(param,param.get("orgId").toString());
        }
        return gIdList;
    }

}
